package com.base.controller;

import com.fruits.constant.FruitsConstant;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Table;
import com.jfinal.plugin.activerecord.TableMapping;

/**
 * 超级控制器
 * 
 * @author chenxl
 * 
 * @date 2015-1-16
 * @time 下午06:10:22
 */
public abstract class BaseController extends Controller {

	/** 实体类 */
	protected abstract Model<?> getDao();

	private String select;

	private String sqlExceptSelect;

	private Object[] sqlValues = new Object[] {};

	private String modelName;

	public String getModelName() {
		return this.modelName;
	}

	{
		this.modelName = getDao().getClass().getSimpleName().toLowerCase();
		// setAttr("bean", modelName);
	}

	// /** 别名 */
	// protected String getAlias() {
	// return getModelName();
	// }

	public void setSqlConfig(String select, String sqlExceptSelect, Object[] sqlValues) {
		this.select = select;
		this.sqlExceptSelect = sqlExceptSelect;
		this.sqlValues = sqlValues;
	}

	/** model名称 */
	// protected String getModelName() {
	// String modelName = getDao().getClass().getSimpleName().toLowerCase();
	// setAttr("bean", modelName);
	// return modelName;
	// }

	/**
	 * 当前映射的表信息
	 * 
	 * @return
	 */
	public Table getTable() {
		return TableMapping.me().getTable(getDao().getClass());
	}

	/**
	 * 保存/更新
	 */
	public void save() {
		Model<?> model = getModel(getDao().getClass(), modelName);
		if (model.get("id") == null) {
			model.save();
		} else {
			model.update();
		}
		forwardAction("/pages/" + modelName + "/list");
	}

	public void update() {
		getModel(getDao().getClass(), modelName).update();
		renderText("update 1");
	}

	/**
	 * 删除
	 */
	public void delete() {
		String dm = getPara(0);
		if (StrKit.notBlank(dm)) {
			getDao().deleteById(dm);
		}
	}

	/**
	 * 删除多个主键
	 */
	public void delByZjs() {
		String zj = getPara(0);
		if (StrKit.notBlank(zj)) {
			StringBuffer sb = new StringBuffer();
			sb.append(" delete from ").append(getTable().getName());
			sb.append(" where zj in('").append(zj.replace(",", "','")).append("')");
			Db.update(sb.toString());
		}
		forwardAction("/pages/" + modelName + "/list");
		// renderText("ok");
	}

	/**
	 * 查找单位对象
	 */
	public void findById() {
		Model<?> model = getModel(getDao().getClass(), modelName);
		model = getDao().findById(model.get("id"));
		setAttr(modelName, model);
		renderFreeMarker("/pages/model/" + modelName + "/" + modelName + "_insert.ftl");
		// renderJson(model);
	}

	/**
	 * 跳转到form页面
	 */
	public void form() {
		Model<?> model = getModel(getDao().getClass(), modelName);
		if (model.get("id") != null) {
			setAttr(modelName, getDao().findById(model.get("id")));
		}
		renderFreeMarker("/pages/model/" + modelName + "/" + modelName + "_insert.ftl");
	}

	/**
	 * 分页页面
	 */
	public void list() {
		Page<Model<?>> page = null;
		if (this.select != null && this.sqlExceptSelect != null) {
			page = paginate(this.select, this.sqlExceptSelect, this.sqlValues);
		} else {
			page = paginate("select * ", " from " + getTable().getName(), new Object[] {});
		}
		setAttr("page", page);
		renderFreeMarker("/pages/model/" + modelName + "/" + modelName + "_list.ftl");
	}

	/**
	 * 分页
	 * 
	 * @param select
	 * @param sqlExceptSelect
	 * @param arr
	 */
	@SuppressWarnings("unchecked")
	private Page<Model<?>> paginate(String select, String sqlExceptSelect, Object... arr) {
		// try {
		int pageNumber = getParaToInt("page") == null ? 1 : getParaToInt("page");
		int pageSize = getParaToInt("rows") == null ? FruitsConstant.ROWS : getParaToInt("rows");
		return (Page<Model<?>>) getDao().paginate(pageNumber, pageSize, select, sqlExceptSelect, arr);
		// return paging;
		// renderText(JsonKit.toJson(paging));
		// Map<String, Object> jsonMap = new HashMap<String, Object>();
		// jsonMap.put("total", paging.getTotalRow());
		// jsonMap.put("rows", paging.getList());

		// } catch (Exception e) {
		// e.printStackTrace();
		// }

	}

}
